package cn.com.gridinfo.sooc.core.course.classes.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.Log;

import java.util.List;

import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseKinds;

/**
 * Created by jieli on 15/12/10.
 */
public class CourseKindsAdapter extends BaseExpandableListAdapter {
    List<CourseKinds> cklist;
    Context context;
    private LayoutInflater inflater;

    public CourseKindsAdapter(Context context, List<CourseKinds> cklist) {
        this.cklist = cklist;

        inflater = ((Activity) context).getLayoutInflater();
    }

    @Override
    public int getGroupCount() {
        return cklist.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return cklist.get(groupPosition).getSub().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return cklist.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return cklist.get(groupPosition).getSub().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        TextView title;
        ImageView parentArrow;
        TextView count;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.course_kinds_list_parent_item, parent, false);
        }
        title = (TextView) convertView.findViewById(R.id.parentName);
        parentArrow = (ImageView) convertView.findViewById(R.id.parentArrow);
        title.setText(cklist.get(groupPosition).getXsm() + "（" + cklist.get(groupPosition).getCount() + "）");

        if (isExpanded) {
            parentArrow.setImageResource(R.mipmap.ic_arrow_up);
        } else {
            parentArrow.setImageResource(R.mipmap.ic_arrow_down);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView title;
        TextView count;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.course_kinds_list_children_item, parent, false);
        }
        title = (TextView) convertView.findViewById(R.id.childrenName);
        count = (TextView) convertView.findViewById(R.id.count);

        String childName = cklist.get(groupPosition).getSub().get(childPosition).getXsm();
        title.setText(childName);
        count.setText(cklist.get(groupPosition).getSub().get(childPosition).getCount());

        //最后的分割线长显
        View line = convertView.findViewById(R.id.course_kind_line);
        if (cklist.get(groupPosition).getSub().size() - 1 == childPosition) {
            line.setVisibility(View.VISIBLE);
        } else {
            line.setVisibility(View.GONE);
        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        Log.d("isChildSelectable");
        return true;
    }
}
